
package com.hzf.booklibrary.ui.activity;


import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.hzf.booklibrary.R;
import com.hzf.booklibrary.base.BaseActivity;
import com.hzf.booklibrary.base.Constant;
import com.hzf.booklibrary.component.AppComponent;
import com.hzf.booklibrary.component.DaggerMainComponent;
import com.hzf.booklibrary.manager.CacheManager;
import com.hzf.booklibrary.utils.RestartAPPTool;
import com.hzf.booklibrary.utils.SharedPreferencesUtil;
import com.hzf.booklibrary.utils.ToastUtils;

import butterknife.Bind;
import butterknife.OnClick;

/**
 *
 */
public class SettingActivity extends BaseActivity {

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, SettingActivity.class));
    }
    @Bind(R.id.mRadioGroup)
    RadioGroup mRadioGroup;//设置分页效果
    @Bind(R.id.tvCacheSize)
    TextView mTvCacheSize;//缓存数据大小

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_setting;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initTitleBar() {
        mBtnLeft.setText("返回");
        mBtnLeft.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.img_back_left),null,null,null);
        mBtnRight.setVisibility(View.GONE);
        tvCenter.setBackgroundResource(R.drawable.img_title_setting);
    }
    @OnClick(R.id.btn_left)
    public void onClickLeft(){
        finish();
    }

    @Override
    public void initDatas() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String cachesize = CacheManager.getInstance().getCacheSize();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvCacheSize.setText(cachesize);
                    }
                });
            }
        }).start();
        switch (SharedPreferencesUtil.getInstance().getInt(Constant.FLIP_STYLE, 0)){
            case 1:
                mRadioGroup.check(R.id.rb_slide);
                break;
            case 0:
                mRadioGroup.check(R.id.rb_simulation);
                break;
        }
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_slide://滑动效果
                        SharedPreferencesUtil.getInstance().putInt(Constant.FLIP_STYLE,1);
                        break;
                    case R.id.rb_simulation://仿真效果
                        SharedPreferencesUtil.getInstance().putInt(Constant.FLIP_STYLE,0);
                        break;
                }
            }
        });
    }

    @Override
    public void configViews() {
        hideStatusBar();
    }
    //关于我们
    @OnClick(R.id.aboutUs)
    public void onClickAboutUs(){
        startActivity(new Intent(this,AboutUsActivity.class));
    }

    //版本更新
    @OnClick(R.id.mServerSetting)
    public void onClickUpdate(){
        View view = getLayoutInflater().inflate(R.layout.common_input_pwd,null);
        final EditText etPwd = (EditText) view.findViewById(R.id.et_inputPwd);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setCancelable(false);
        final AlertDialog dialog = builder.create();
        view.findViewById(R.id.btn_confirm_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if("huazhifan2017".equals(etPwd.getText().toString())){
                    showIpDialog();//出现输入Ip和端口号
                    dialog.dismiss();
                }else {
                    ToastUtils.showToast("密码不正确");
                }
            }
        });
        view.findViewById(R.id.btn_cancel_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setView(view);

        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        dialog.show();
    }

    private void showIpDialog() {
        View view = getLayoutInflater().inflate(R.layout.common_server_setting,null);
        final EditText etIP = (EditText) view.findViewById(R.id.et_inputIP);
        final EditText etPORT = (EditText) view.findViewById(R.id.et_inputPORT);
        etIP.setText(SharedPreferencesUtil.getInstance().getString("ip",""));
        etPORT.setText(SharedPreferencesUtil.getInstance().getString("port",""));
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setCancelable(false);
        final AlertDialog dialog = builder.create();
        view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferencesUtil.getInstance().putString("ip",etIP.getText().toString());
                SharedPreferencesUtil.getInstance().putString("port",etPORT.getText().toString());
                RestartAPPTool.restartAPP(SettingActivity.this);//重启app
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
//        Window window = dialog.getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.alpha = 0.5f;
//        window.setAttributes(lp);
        dialog.show();
    }

    //清空缓存
    @OnClick(R.id.cleanCache)
    public void onClickCleanCache() {
        //默认不勾选清空书架列表，防手抖！！
        View view = getLayoutInflater().inflate(R.layout.common_clear_cache,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext).setCancelable(false);
        final AlertDialog dialog = builder.create();
        view.findViewById(R.id.btn_confirm_cache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        CacheManager.getInstance().clearCache(false, true);//清除图书和阅读缓存
                        final String cacheSize = CacheManager.getInstance().getCacheSize();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTvCacheSize.setText(cacheSize);
                               // EventBus.getDefault().post(new RefreshCollectionListEvent());
                            }
                        });
                    }
                }).start();
                dialog.dismiss();
            }
        });
        view.findViewById(R.id.btn_cancel_cache).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
//        Window window = dialog.getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.alpha = 0.5f;
//        window.setAttributes(lp);
        dialog.show();
    }
}
